//
//  LGRecommendServiceCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/3/24.
//

#import "LGRecomendServiceCell.h"

@interface LGRecomendServiceCell()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UILabel *typeLabel;
/**   */
@property (nonatomic , strong)UILabel *priceLabel;
/**   */
@property (nonatomic , strong)UIView *bottomView;
/**   */
@property (nonatomic , strong)UIButton *detailBtn;

@end

@implementation LGRecomendServiceCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.titleLabel.text = dataDic[@"title"];
    self.priceLabel.text = [NSString stringWithFormat:@"¥ %@",dataDic[@"pirce"]];
    for (UIButton *button in self.bottomView.subviews) {
        [button removeFromSuperview];
    }
    NSArray *itemArray = dataDic[@"desc"];
    CGFloat itemWidth = viewPix(105);
    CGFloat itemHeight = viewPix(20);
    CGFloat margin = viewPix(15);
    for (NSInteger i=0; i<itemArray.count; i++) {
        UIButton *button = [UIButton buttonWithTitle:itemArray[i] titleFont:LGFont(12) textColor:@"#0754AD" imageName:@"推荐服务-服务内容"];
        button.frame = CGRectMake(viewPix(16)+(itemWidth+margin)*(i%2), viewPix(5)+itemHeight*(i/2), itemWidth, itemHeight);
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        button.titleEdgeInsets = UIEdgeInsetsMake(0, 3, 0, 0);
        [self.bottomView addSubview:button];
        if(i == itemArray.count-1){
            [self.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(kMaxY(button.frame)+viewPix(8)));
            }];
        }
    }
    NSInteger lineNum = itemArray.count/2+itemArray.count%2;
    CGFloat height = (lineNum>0)?(lineNum*itemHeight+viewPix(16)):0;
    [self.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(height));
    }];
    self.detailBtn.hidden = (itemArray.count>0)?NO:YES;
}


-(void)createSubView{
    self.contentView.backgroundColor = [UIColor colorWithString:@"#F9F9F9"];
    [self.contentView addSubview:self.baseView];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.typeLabel];
    [self.baseView addSubview:self.priceLabel];
    [self.baseView addSubview:self.selectBtn];
    [self.baseView addSubview:self.bottomView];
    [self.baseView addSubview:self.detailBtn];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(8));
        make.left.equalTo(self.contentView).offset(viewPix(16));
        make.right.equalTo(self.contentView).offset(-viewPix(16));
        make.bottom.equalTo(self.contentView).offset(-viewPix(8));
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.baseView).offset(viewPix(16));
        make.left.equalTo(self.baseView).offset(viewPix(47));
        make.right.equalTo(self.baseView).offset(-viewPix(12));
    }];
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.baseView).offset(-viewPix(12));
        make.centerY.equalTo(self.titleLabel);
    }];
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(viewPix(7));
        make.left.equalTo(self.titleLabel);
    }];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.priceLabel.mas_bottom).offset(viewPix(5));
        make.left.equalTo(self.titleLabel);
        make.right.equalTo(self.baseView).offset(-viewPix(12));
        make.bottom.equalTo(self.baseView).offset(-viewPix(12));
        make.height.equalTo(@(viewPix(54)));
    }];
    [self.detailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bottomView).offset(-viewPix(12));
        make.centerY.equalTo(self.bottomView);
        make.width.equalTo(@(viewPix(50)));
        make.height.equalTo(@(viewPix(20)));
    }];
    [self.selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.centerY.equalTo(self.baseView);
        make.width.height.equalTo(@(viewPix(47)));
    }];
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc] init];
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.cornerRidus = 15;
    }
    return _baseView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _titleLabel;
}

-(UILabel *)typeLabel{
    if(!_typeLabel){
        _typeLabel = [UILabel lableWithText:@"" colorString:@"#3E7CAC" textFont:LGFontWeight(14, UIFontWeightMedium) textAlignment:NSTextAlignmentRight lines:1];
    }
    return _typeLabel;
}

-(UILabel *)priceLabel{
    if(!_priceLabel){
        _priceLabel = [UILabel lableWithText:@"" colorString:@"#3E7CAC" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _priceLabel;
}

-(UIButton *)selectBtn{
    if(!_selectBtn){
        _selectBtn = [UIButton buttonWithImage:@"unselect24" selectImage:@"selected24"];
        _selectBtn.frame = CGRectMake(0, viewPix(44), viewPix(48), viewPix(48));
    }
    return _selectBtn;
}

-(UIView *)bottomView{
    if(!_bottomView){
        _bottomView = [[UIView alloc]init];
        _bottomView.backgroundColor = [UIColor colorWithString:@"#F7F9FB"];
        _bottomView.cornerRidus = 5;
    }
    return _bottomView;
}

-(UIButton *)detailBtn{
    if(!_detailBtn){
        _detailBtn = [UIButton buttonWithTitle:@"详情" titleFont:LGFont(13) textColor:@"#0754AD" imageName:@"grayTip12"];
        _detailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        _detailBtn.bounds = CGRectMake(0, 0, viewPix(50), viewPix(20));
        [_detailBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:2];
    }
    return _detailBtn;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
